﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity.Validation;
using HYI.SDBSERVER;
using eHCBaseClass;
using Sunny.UI;

namespace SHRSYSTEM
{
    public partial class FMMENU : SDBSERVER.FMSINGLE
    {
        //入口函数
        public static Int32 StartMe(string sMainPara, string sPara, string sUser, DBSERVER sDB)
        {
            StaticClass.Selectm_MainPara = sMainPara;
            StaticClass.SelectItemValue = sPara;
            StaticClass.SelectUser = sUser;
            StaticClass.myDB = sDB;
            return 0;
        }

        private string sType = "V";
        private string sSite_No = string.Empty;
        //DBSERVER myADO = new DBSERVER();
        DBCache myCache = new DBCache();
        private int iGridView = 0;

        public FMMENU()
        {
            InitializeComponent();
        }

        //查询代码
        protected override void OnSys04()
        {
            string SQL = "select b.fMenuCaption fParentName, a.* "
              + " from sys_menu a"
              + "  left join sys_menu b on b.fMenuId = a.fParentId"
              + "  where 1 = 1";
            DataRowView drv = (DataRowView)cbfParentId.SelectedItem;
            if ((drv != null) && (cbfParentId.SelectedIndex > 0))
            {
                string sfParentId = drv.Row["fmenuid"].ToString();
                SQL = SQL + " and a.fParentId='" + sfParentId + "'";
            }
            if (qifMenuId.Text != "")
            {
                SQL = SQL + " and ((a.fMenuCaption like '%" + qifMenuId.Text + "%') or (a.fMenuId like '%" + qifMenuId.Text + "%'))";
            }
            SQL = SQL + "  order by fParentId,fSeqNo,fMenuId";
            Qrytable = StaticClass.myDB.query(SQL);

            base.OnSys04();
            
            rowindex = -1;
        }

        private void FMMENU_Load(object sender, EventArgs e)
        {
            sUser = StaticClass.SelectUser;
            string[] strArray = StaticClass.Selectm_MainPara.Split(new char[] { ';' });
            sMenu = strArray[1];
            sSite_No = strArray[0];

            //按钮控制
            sys01.Visible = false; sys01.Tag = "1";
            sys03.Visible = false; sys03.Tag = "1";
            sys05.Visible = false; sys05.Tag = "1";
            sys06.Visible = false; sys06.Tag = "1";
            sys07.Visible = false; sys07.Tag = "1";
            sys11.Visible = false; sys11.Tag = "1";

            //下拉框
            DataTable myGroup = StaticClass.myDB.query("select fmenuid,fmenucaption from sys_menu where fparentid=0 order by fSeqNo");
            uifParentId.DataSource = myGroup;
            uifParentId.DisplayMember = "fmenucaption";
            uifParentId.ValueMember = "fmenuid";

            DataRow dr = myGroup.NewRow();
            dr["fmenucaption"] = "请选择";
            dr["fmenuid"] = "0";
            myGroup.Rows.InsertAt(dr, 0);
            cbfParentId.DataSource = myGroup;
            cbfParentId.DisplayMember = "fmenucaption";
            cbfParentId.ValueMember = "fmenuid";
            cbfParentId.SelectedIndex = 0;

            DataTable myTs_Dict = myCache.doGetDict("S_TS_DICT", "fDictType=1000");
            uifVisible.DataSource = myTs_Dict;
            uifVisible.DisplayMember = "fDictName";
            uifVisible.ValueMember = "fDictNo";

            //执行查询代码
            OnSys04();

            //表格列显示统一控制
            if (iGridView == 0)
            {
                myCache.doSetGridVIew(sMenu, sUser, uiDGView);
                iGridView = iGridView + 1;
            }
        }

        //新增代码
        protected override void OnSys01()
        {
            uifMenuId.Text = "";
            uifMenuCaption.Text = "";
            uifParentId.Text = "";
            uifPackage.Text = "";
            uifFormName.Text = "";
            uifFormParams.Text = "";
            uifVisible.SelectedValue = "1";

            sType = "A";
            doLoadData();
            uifMenuId.ReadOnly = false;

            base.OnSys01();
        }

        //编辑+浏览载入数据
        private void doLoadData()
        {
            //控制界面输入
            Boolean input = false;
            if ((sType.Equals("A")) || (sType.Equals("B")))
            {
                input = false;
            }
            else
            {
                input = true;
            }
            foreach (Control ctrl in uiPanelData.Controls)
            {
                if (ctrl is UITextBox)
                {
                    UITextBox m = (UITextBox)ctrl;
                    m.ReadOnly = input;
                }

                if (ctrl is UIComboBox)
                {
                    UIComboBox m = (UIComboBox)ctrl;
                    m.ReadOnly = input;
                }
            }

            if (!sType.Equals("A"))
            {
                uifMenuId.Text = uiDGView["fMenuId", rowindex].Value.ToString();
                uifMenuCaption.Text = uiDGView["fMenuCaption", rowindex].Value.ToString();
                uifPackage.Text = uiDGView["fPackage", rowindex].Value.ToString();
                uifFormName.Text = uiDGView["fFormName", rowindex].Value.ToString();
                uifFormParams.Text = uiDGView["fFormParams", rowindex].Value.ToString();
                uifSeqNo.Text = uiDGView["fSeqNo", rowindex].Value.ToString();

                int i = 0;
                string sfVisible = uiDGView["fVisible", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifVisible.Items)
                {
                    string id = dr["fDictNo"].ToString();
                    if (id.Equals(sfVisible))
                    {
                        uifVisible.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }

                i = 0;
                string sfParentId = uiDGView["fParentId", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifParentId.Items)
                {
                    string id = dr["fMenuId"].ToString();
                    if (id.Equals(sfParentId))
                    {
                        uifParentId.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }
            }
        }

        //编辑代码
        protected override void OnSys02()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }

            sType = "B";
            doLoadData();
            
            uifMenuId.ReadOnly = true;

            base.OnSys02();
        }


        //浏览代码
        protected override void OnSys10()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }

            sType = "V";

            doLoadData();

            base.OnSys10();

        }

        //数据为空检查
        private bool CheckData()
        {
            return CheckEmpty(uifMenuId, "请输入功能编号")
                   && CheckEmpty(uifMenuCaption, "请输入功能名称")
                   && CheckEmpty(uifSeqNo, "请输入功能顺序")
                   && CheckEmpty(uifVisible, "请选择功能状态");
        }

        //保存代码
        protected override void OnSys08()
        {
            if (!CheckData())
            {
                return;
            }
            DataRowView drv1 = (DataRowView)uifVisible.SelectedItem;
            string sfVisible = drv1.Row["fDictNo"].ToString();

            string sfParentId = "0";
            DataRowView drv2 = (DataRowView)uifParentId.SelectedItem;
            if (drv2 != null)
            {
                sfParentId = drv2.Row["fmenuid"].ToString();
            }

            if (sType == "A")
            {
                try
                {
                    using (XTBasEntities context = new XTBasEntities())
                    {
                        sys_menu myData = context.sys_menu.Find(int.Parse(uifMenuId.Text));
                        if (myData is null)
                        {
                            sys_menu myData2 = new sys_menu();
                            myData2.fMenuType = "1";
                            myData2.fMenuId = int.Parse(uifMenuId.Text);
                            myData2.fMenuCaption = uifMenuCaption.Text;
                            myData2.fParentId = int.Parse(sfParentId);
                            myData2.fActionType = 0;
                            myData2.fPackage = uifPackage.Text;
                            myData2.fFormName = uifFormName.Text;
                            myData2.fFormParams = uifFormParams.Text;
                            myData2.fShowStyle = 0;
                            myData2.fWidth = 0;
                            myData2.fHeight = 0;
                            myData2.fSeqNo = int.Parse(uifSeqNo.Text);
                            myData2.fVisible = int.Parse(sfVisible);
                            context.sys_menu.Add(myData2);
                            context.SaveChanges();
                            doUpCache("AM_SYS_USER");
                            UIMessageTip.ShowOk("添加成功");

                        }
                        else
                        {
                            //Console.WriteLine("添加成功");
                            UIMessageTip.ShowOk("编号已经存在");
                            //Console.WriteLine("已存在");
                            return;
                        }

                    }
                }
                catch (DbEntityValidationException dbEx)
                {
                    Console.WriteLine("错误" + dbEx.Message);
                }
            }

            else if (sType == "B")
            {
                using (XTBasEntities context = new XTBasEntities())
                {
                    //查询
                    sys_menu mysys_menu = context.sys_menu.Find(int.Parse(uifMenuId.Text));

                    mysys_menu.fMenuCaption = uifMenuCaption.Text;
                    mysys_menu.fParentId = int.Parse(sfParentId);
                    mysys_menu.fPackage = uifPackage.Text;
                    mysys_menu.fFormName = uifFormName.Text;
                    mysys_menu.fFormParams = uifFormParams.Text;
                    mysys_menu.fSeqNo = int.Parse(uifSeqNo.Text);
                    mysys_menu.fVisible = int.Parse(sfVisible);
                    int result = context.SaveChanges();
                    doUpCache("AM_SYS_USER");
                    UIMessageTip.ShowOk("修改成功");
                }
            }

            //刷新cache
            sType = "V";
            base.OnSys08();

            OnSys04();
        }

        private void cbfParentId_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }
    }
}
